<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="android mio4kon mio">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
          Appium踩坑之旅——小米手机 - Mio4kon的博客 | Mio4kon&#39;s Blog
        
    </title>

    <link rel="canonical" href="http://mio4kon.com/2017/06/12/Appium踩坑之旅——小米手机/">

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="/css/bootstrap.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/hux-blog.min.css">

    <!-- Pygments Highlight CSS -->
    <link rel="stylesheet" href="/css/highlight.css">

    <!-- Custom Fonts -->
    <!-- <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"> -->
    <!-- Hux change font-awesome CDN to qiniu -->
    <link href="https://cdn.staticfile.org/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">


    <!-- Hux Delete, sad but pending in China
    <link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/
    css'>
    -->


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- ga & ba script hoook -->
    <script></script>
</head>


<!-- hack iOS CSS :active style -->
<body ontouchstart="">

    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">Mio4kon</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">Home</a>
                    </li>

                    

                        
                    

                        
                        <li>
                            <a href="/about/">About</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/archives/">Archives</a>
                        </li>
                        
                    

                        
                        <li>
                            <a href="/tags/">Tags</a>
                        </li>
                        
                    
                    
                </ul>
            </div>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>


    <!-- Main Content -->
    
<!-- Image to hack wechat -->
<!-- <img src="http://mio4kon.com/img/icon_wechat.png" width="0" height="0"> -->
<!-- <img src="{{ site.baseurl }}/{% if page.header-img %}{{ page.header-img }}{% else %}{{ site.header-img }}{% endif %}" width="0" height="0"> -->

<!-- Post Header -->
<style type="text/css">
    header.intro-header{
        background-image: url('/img/home-bg.jpg')
    }
</style>
<header class="intro-header" >
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <div class="tags">
                        
                          <a class="tag" href="/tags/#移动自动化测试" title="移动自动化测试">移动自动化测试</a>
                        
                    </div>
                    <h1>Appium踩坑之旅——小米手机</h1>
                    <h2 class="subheading">踩坑之旅</h2>
                    <span class="meta">
                        Posted by Mio4kon on
                        2017-06-12
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

    <!-- Post Container -->
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

                <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>了解过 <code>Appium</code> 或看过我之前写的系列教程的人,一定知道在与<code>Appium Server</code>连接的时候会传入相应的<code>capabilities</code>.</p>
<ul>
<li>platformName</li>
<li>automationName</li>
<li>autoLaunch</li>
<li>noSign</li>
<li>….</li>
</ul>
<p><code>automationName</code>是告诉 <code>Appium Server</code>是用什么方式来操作UI元素的.对应源码来说就是使用什么样的 <code>Driver</code>.(下一篇教程会简单的过一遍 <code>Appium Server</code>的源码)</p>
<p>由于<code>uiautomator2</code>的执行效率以及<strong>支持<code>Toast</code>验证</strong>.使得在写测试用例的时候应优先使用<code>uiautomator2</code>.</p>
<p>不过当我使用<code>uiautomator2</code>后,尤其是小米手机,那可是一坑接一坑..</p>
<h1 id="坑一-autoLaunch"><a href="#坑一-autoLaunch" class="headerlink" title="坑一:autoLaunch"></a>坑一:autoLaunch</h1><p>由于公司的平台希望能在跑Case前由平台来控制打开App.所以我将<code>autoLaunch</code>设置为<code>false</code>.起初在我的乐视手机上没有任何问题.但是当换到一台<strong>未跑过测试代码的小米手机</strong>时候,发现一只报一个奇葩的错误:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[MJSONWP] Encountered internal error running command: ProxyRequestError: Could not proxy command to remote server. Original error: Error: socket hang up</div><div class="line">    at JWProxy.proxy$ (../../../lib/jsonwp-proxy/proxy.js:153:13)</div><div class="line">    at tryCatch (/Users/mio4kon/code/mio4kon_github/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)</div><div class="line">    at GeneratorFunctionPrototype.invoke [as _invoke] (/Users/mio4kon/code/mio4kon_github/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)</div><div class="line">    at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/Users/mio4kon/code/mio4kon_github/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)</div><div class="line">    at GeneratorFunctionPrototype.invoke (/Users/mio4kon/code/mio4kon_github/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)</div><div class="line">[HTTP] &lt;-- POST /wd/hub/session 500 31412 ms - 216</div></pre></td></tr></table></figure>
<p>好吧.当时也不知道为啥我就试着把<code>uiautomator2</code>这条删掉.发现一切OK了.竟然能跑起来了.根据多年的控制变量法我得出了”看来小米手机可能不支持uiautomator2吧”. native </p>
<p>于是后来我就为了兼容小米手机对<code>case</code>做了适配.但是不能验证<code>Toast</code>这个问题就变得很棘手了.所以还要避免用<code>Toast</code>来验证<code>case</code>的成功还是失败.再后来我发现小米手机在使用<code>uiautomator1</code>的时候.会存在反馈特别慢甚至会超时或者找不到等等的问题.在<code>Appium</code>上提<code>ISSUE</code>得到的反馈是使用<code>uiautomator2</code>再试试看.Orz…</p>
<p>还是得动手靠自己.就此我打算研究下 <code>Appium Server</code>的源码,想要解决的问题有下面两点:</p>
<ol>
<li>为什么小米手机在用 <code>uiautomator1</code> 的时候会存在服务端返回超时或找不到元素的情况(必现)</li>
<li>为什么小米手机Api&gt;18却不支持 <code>uiautomator2</code>呢?是不是我使用姿势的问题?</li>
</ol>
<p>带着原因我开始翻源码了(这篇文章不过多涉及源码.主要说结论)</p>
<p>在说这两个问题之前先说明一个问题:之所以<code>AppiumServer</code>可以控制手机的UI,是因为<code>Appium</code>会在手机上开一个<code>Server</code>,通过<code>AppiumServer</code>与<code>PhoneServer</code>之间的通讯来控制手机UI.</p>
<p>第一个问题.Debug发现<code>AppiumServer</code>与<code>PhoneServer</code>之间的通讯没有问题.问题主要在于调用<code>uiautomator1</code>的api时抛了异常.但是这个<code>PhoneServer</code>的工程编译各种问题.所以第一个问题暂时先放下.暂时归结于<code>uiautomator1</code>不稳定.有兴趣的小伙伴可以看看这个<code>PhoneServer</code>的源码.</p>
<p><a href="https://github.com/appium/appium-android-bootstrap" target="_blank" rel="external">https://github.com/appium/appium-android-bootstrap</a></p>
<p>于是我开始把主要精力放在第二个问题上.因为如果解决第二个问题.那么<code>Toast</code>验证的问题就迎刃而解了.而且现在主流的手机都在<code>Api18</code>以上,可以放心使用<code>uiautomator2</code>.</p>
<p>直接上源码:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">[appium-uiautomator2-driver:driver.js]</div><div class="line"></div><div class="line">    // If the user sets autoLaunch to false, they are responsible for initAUT() and startAUT()</div><div class="line">    if (this.opts.autoLaunch) &#123;</div><div class="line">      // set up app under test</div><div class="line">      // prepare our actual AUT, get it on the device, etc...</div><div class="line">      await this.initAUT();</div><div class="line">&#125;</div><div class="line"></div><div class="line">  async initAUT () &#123;</div><div class="line">  		....</div><div class="line">    await this.grantPermissions();</div><div class="line">    await this.uiautomator2.installServerApk();</div><div class="line">  &#125;</div></pre></td></tr></table></figure>
<p>可以发现只要autoLaunch为false,就不会安装ServerApk…</p>
<p>问题终于找到了.我自己装还不行嘛!! 好了有遇到下一个坑.</p>
<h1 id="坑二-神隐模式"><a href="#坑二-神隐模式" class="headerlink" title="坑二:神隐模式"></a>坑二:神隐模式</h1><p>当我安装好了ServerApk,再跑用例的时候发现另一个神奇的问题.</p>
<p><img src="http://mio4kon.qiniudn.com/14982050849988.jpg" alt=""><br><img src="http://mio4kon.qiniudn.com/14982051025805.jpg" alt=""></p>
<p>好吧.又报了个这个错.不过这次过程明显感到不一样.这次可以明细感觉比之前要慢很多.这是因为实际上<code>uiautomator2</code>的服务还在运行.但是始终连不上.</p>
<p>为什么连不上呢?由于这个是小米手机.而小米有一个<a href="https://jingyan.baidu.com/article/c275f6ba2ac144e33d756731.html" target="_blank" rel="external">神隐模式</a>.简单来说就是后台的应用禁止网络.而<code>uiautomator2</code>的服务恰好是通过代理转发返回响应消息的.这就使得服务一直没有响应,最终报错.</p>
<p>所有一定要把<code>uiautomator2Server</code>加入神隐模式的白名单,或者关闭神隐模式.</p>
<h1 id="坑三-INJECT-EVENTS"><a href="#坑三-INJECT-EVENTS" class="headerlink" title="坑三:INJECT_EVENTS"></a>坑三:INJECT_EVENTS</h1><p>解决了上面两个坑.你会发现可以顺利的打开App进行测试了.你以为这就完了?<br>跑完用例你会发现所有的case全部失败了.什么鬼?我用<code>uiautomator1</code>跑用例好好的.怎么一换成<code>uiautomator2</code>全是失败?</p>
<p>case最后会发现错误信息:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">selenium.common.exceptions.WebDriverException: Message: An unknown server-side </div><div class="line">error occurred while processing the command. Original error: </div><div class="line">java.lang.SecurityException: Injecting to another application requires </div><div class="line">INJECT_EVENTS permission</div></pre></td></tr></table></figure>
<p><code>INJECT_EVENTS permission</code>又是什么鬼.</p>
<p>网上搜了一下才发现小米手机需要打开<code>USB调试（安全设置）</code>才可以.但是之前没打开这个设置<code>uiautomator1</code>好像也并没有什么问题.</p>
<p>总之在开发者模式中打开这个选项.你就可以放心的在小米手机上使用<code>uiautomator2</code>了.</p>
<p>这三个坑虽然解决起来相当简单.但是当你第一次遇到的时候真的是一脸懵逼.尤其是前面两个坑.网上都找不到原因的.只能靠半猜半试来寻找原因.</p>
<p>下一篇将会对 <code>appium server</code> 的源码进行分析.梳理 <code>server</code>的流程,有助于解决一些莫名其妙的问题.</p>


                <hr>

                

                <ul class="pager">
                    
                        <li class="previous">
                            <a href="/2017/06/18/Appium源码解析——建立连接/" data-toggle="tooltip" data-placement="top" title="Appium源码解析之一——建立连接">&larr; Previous Post</a>
                        </li>
                    
                    
                        <li class="next">
                            <a href="/2017/04/13/移动端自动化测试系列之五——AppiumLich框架使用/" data-toggle="tooltip" data-placement="top" title="移动端自动化测试系列之五——AppiumLich框架使用">Next Post &rarr;</a>
                        </li>
                    
                </ul>

                

                
                <!-- disqus 评论框 start -->
                <div class="comment">
                    <div id="disqus_thread" class="disqus-thread"></div>
                </div>
                <!-- disqus 评论框 end -->
                

            </div>
    <!-- Side Catalog Container -->
        
            <div class="
                col-lg-2 col-lg-offset-0
                visible-lg-block
                sidebar-container
                catalog-container">
                <div class="side-catalog">
                    <hr class="hidden-sm hidden-xs">
                    <h5>
                        <a class="catalog-toggle" href="#">CATALOG</a>
                    </h5>
                    <ul class="catalog-body"></ul>
                </div>
            </div>
        

    <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                
                <section>
                    <!-- no hr -->
                    <h5><a href="/tags/">FEATURED TAGS</a></h5>
                    <div class="tags">
                       
                          <a class="tag" href="/tags/#移动自动化测试" title="移动自动化测试">移动自动化测试</a>
                        
                    </div>
                </section>
                

                <!-- Friends Blog -->
                
                <hr>
                <h5>FRIENDS</h5>
                <ul class="list-inline">

                    
                        <li><a href="http://lrd.ele.me/" target="_blank">lrd ele&#39;s Blog</a></li>
                    
                </ul>
                
            </div>

        </div>
    </div>
</article>




<!-- disqus 公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
    /* * * CONFIGURATION VARIABLES * * */
    var disqus_shortname = "hexo-theme-huxblog";
    var disqus_identifier = "http://mio4kon.com/2017/06/12/Appium踩坑之旅——小米手机/";
    var disqus_url = "http://mio4kon.com/2017/06/12/Appium踩坑之旅——小米手机/";

    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<!-- disqus 公共JS代码 end -->




<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>
<!-- anchor-js, Doc:http://bryanbraun.github.io/anchorjs/ -->
<script>
    async("https://cdn.bootcss.com/anchor-js/1.1.1/anchor.min.js",function(){
        anchors.options = {
          visible: 'always',
          placement: 'right',
          icon: '#'
        };
        anchors.add().remove('.intro-header h1').remove('.subheading').remove('.sidebar-container h5');
    })
</script>
<style>
    /* place left on bigger screen */
    @media all and (min-width: 800px) {
        .anchorjs-link{
            position: absolute;
            left: -0.75em;
            font-size: 1.1em;
            margin-top : -0.1em;
        }
    }
</style>



    <!-- Footer -->
    <!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                
                
                

                
                    <li>
                        <a target="_blank" href="http://weibo.com/u/3198010790">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-weibo fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                
                    <li>
                        <a target="_blank"  href="https://github.com/Mio4kon">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; Mio4kon 2019 
                    <br>
                    Theme by <a href="http://huangxuan.me">Hux</a> 
                    <span style="display: inline-block; margin: 0 5px;">
                        <i class="fa fa-heart"></i>
                    </span> 
                    Ported by <a href="http://blog.kaijun.rocks">Kaijun</a> | 
                    <iframe
                        style="margin-left: 2px; margin-bottom:-5px;"
                        frameborder="0" scrolling="0" width="91px" height="20px"
                        src="https://ghbtns.com/github-btn.html?user=kaijun&repo=hexo-theme-huxblog&type=star&count=true" >
                    </iframe>
                </p>
            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->
<script src="/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

<!-- Custom Theme JavaScript -->
<script src="/js/hux-blog.min.js"></script>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- 
     Because of the native support for backtick-style fenced code blocks 
     right within the Markdown is landed in Github Pages, 
     From V1.6, There is no need for Highlight.js, 
     so Huxblog drops it officially.

     - https://github.com/blog/2100-github-pages-now-faster-and-simpler-with-jekyll-3-0  
     - https://help.github.com/articles/creating-and-highlighting-code-blocks/    
-->
<!--
    <script>
        async("http://cdn.bootcss.com/highlight.js/8.6/highlight.min.js", function(){
            hljs.initHighlightingOnLoad();
        })
    </script>
    <link href="http://cdn.bootcss.com/highlight.js/8.6/styles/github.min.css" rel="stylesheet">
-->


<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("http://mio4kon.com/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>


<!-- Google Analytics -->




<!-- Baidu Tongji -->


<!-- Side Catalog -->

<script type="text/javascript">
    function generateCatalog (selector) {
        var P = $('div.post-container'),a,n,t,l,i,c;
        a = P.find('h1,h2,h3,h4,h5,h6');
        a.each(function () {
            n = $(this).prop('tagName').toLowerCase();
            i = "#"+$(this).prop('id');
            t = $(this).text();
            c = $('<a href="'+i+'" rel="nofollow">'+t+'</a>');
            l = $('<li class="'+n+'_nav"></li>').append(c);
            $(selector).append(l);
        });
        return true;    
    }

    generateCatalog(".catalog-body");

    // toggle side catalog
    $(".catalog-toggle").click((function(e){
        e.preventDefault();
        $('.side-catalog').toggleClass("fold")
    }))

    /*
     * Doc: https://github.com/davist11/jQuery-One-Page-Nav
     * Fork by Hux to support padding
     */
    async("/js/jquery.nav.js", function () {
        $('.catalog-body').onePageNav({
            currentClass: "active",
            changeHash: !1,
            easing: "swing",
            filter: "",
            scrollSpeed: 700,
            scrollOffset: 0,
            scrollThreshold: .2,
            begin: null,
            end: null,
            scrollChange: null,
            padding: 80
        });
    });
</script>





<!-- Image to hack wechat -->
<img src="http://mio4kon.com/img/icon_wechat.png" width="0" height="0" />
<!-- Migrate from head to bottom, no longer block render and still work -->

</body>

</html>
